package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"hello/gormtest"
)

func main() {
	r := gin.Default()

	r.GET("/hello", hh)

	r.GET("/addData", Add)

	//r.Run("192.168.4.204:9292")
	r.Run(":9292")

}

func hh(context *gin.Context) {
	var i int = 15
	context.JSON(200, gin.H{
		"code":  "200",
		"msg":   "success",
		"data":  "hello",
		"reqId": i,
	})
}

func Add(c *gin.Context) {
	student := &gormtest.Student{
		SId:   "11",
		Sname: "b01",
		Sage:  "2014-06-01 00:00:00",
		Ssex:  "男",
	}

	var s string = "1"
	fmt.Println(s)

	var s1 string = "2"
	fmt.Println(s1)

	s2 := "3"
	fmt.Println(s2)

	dsn := "root:bdyh@2020@tcp(localhost:3306)/test1?charset=utf8mb4&parseTime=True&loc=Local"
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic("Failed to connect to database")
	}

	db.Create(student) // 通过数据的指针来创建
	fmt.Println(student)
	c.String(200, "增加数据成功")

}
